import { createRouter,createWebHashHistory } from 'vue-router'
const routes=[
    {
        path:'/',
        redirect:'/login'
    },
    {
        path:'/login',
        name:'login',
        component:()=>import('@/pages/login/index.vue')
    },
    {
        path:'/reg',
        name:'reg',
        component:()=>import('@/pages/login/reg.vue')
    },
    {
        path:'/home',
        name:'home',
        component:()=>import('@/pages/home/index.vue')
    },
    {
        path:'/userinfo',
        name:'userinfo',
        component:()=>import('@/pages/user/userinfo.vue')
    }
]
const router = createRouter({
    history:createWebHashHistory(),
    routes 
})
router.beforeEach((to, from, next) => {
    const hideLayoutRouteName=['login','reg']
    if(hideLayoutRouteName.includes(to.name)){
        // 如果是登录或者注册 则直接跳转
        next()
    }else if(localStorage.getItem('token')){
        // 如果有token 则跳转对应的页面
        next()
    }else{
        // 没有token 自动跳登录页面
        return { name: 'login' }
        // next('/login')
    }
})
export default router